package com.example.zyfyptlzx729.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zyfyptlzx729.R;
import com.example.zyfyptlzx729.bean.LoginBean;
import com.example.zyfyptlzx729.iface.LoginListener;
import com.example.zyfyptlzx729.model.LoginModel;

public class LoginActivity extends AppCompatActivity {

    private EditText etuser,etpass;
    private Button btlogin;
    private Button btregister;
    private TextView ziyuan;
    private Switch jzmm;
    private String username = "", password = "", sessionID = "";
    private SharedPreferences sp;

    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            sessionID = loginBean.getSessionid();
            Log.i("LoginActivity", "----sessionID=" + sessionID);
            if (sessionID != null) {
                if (jzmm.isChecked()) {
                    saveSP(etuser.getText().toString(),
                            etpass.getText().toString(),
                            sessionID,
                            jzmm.isChecked());
                }
                Toast.makeText(LoginActivity.this, "登录成功" + sessionID, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else {
                saveSP("","","",false);
                Toast.makeText(LoginActivity.this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            saveSP("","","",false);
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };

    private void saveSP(String name, String pass, String session, Boolean state) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("name",name);
        editor.putString("pass",pass);
        editor.putBoolean("state",state);
        editor.putString("session",session);
        editor.commit();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

        sp = getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }

    private void readSP() {
        String name = sp.getString("name","");
        String pass = sp.getString("pass","");
        Boolean state = sp.getBoolean("state",false);
        if(state){
            etuser.setText(name);
            etpass.setText(pass);
        }
        jzmm.setChecked(state);
    }

    private void initView(){
        etuser=findViewById(R.id.etuser);
        etpass=findViewById(R.id.etpass);
        btlogin=findViewById(R.id.button);
        ziyuan=findViewById(R.id.textView);
        btregister=findViewById(R.id.button2);
        jzmm=findViewById(R.id.switch1);
        etpass.setTransformationMethod(PasswordTransformationMethod.getInstance());

        btlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = etuser.getText().toString();
                password = etpass.getText().toString();
                LoginModel loginModel = new LoginModel();

                loginModel.getLoginResult(username, password, loginListener);

            }
        });
        btregister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent_loginActivity_to_RegisterActivityActivity = new Intent(LoginActivity.this, RegisterActivity.class) ;    //切换Login Activity至User Activity
                startActivity(intent_loginActivity_to_RegisterActivityActivity);
                finish();

            }
        });
    }
}
